//
//  JFHomeViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 16/1/14.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//



#import "JFHomeViewController.h"
#import "HomeCollectionViewCell.h"
#import "HomeCollectionHeader.h"
#import "CreditViewController.h"
#import "PaymentAmountController.h"
#import "ApplyViewController.h"
#import "AccountViewController.h"
#import "MyinviteNotesMainController.h"
#import "JCRepaymentViewController.h"
#import "SHCashUnionPaymentController.h"

@interface JFHomeViewController()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,BannerDelegate>


@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout * layout;

@property (nonatomic, strong) NSMutableArray * arrImage;

@end

static NSString * reuseIdentifier = @"HomeCollectionViewCell";


@implementation JFHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;

}

#pragma mark - 设置状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return  UIStatusBarStyleDefault;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSubView];
    
    [SHAppUpdateManager appCheckUpdate];    
}



- (void)initSubView
{
    CGFloat width = (KScreenWidth - 20)/2;
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    
    //  设置 头部的大小
    [self.layout setHeaderReferenceSize:CGSizeMake(KScreenWidth, __I6Height(170))];

    self.layout.itemSize = CGSizeMake(width , __I6Height(125)); //设置item的大小
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.sectionInset = UIEdgeInsetsMake(14, 10, 14, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -KSTATUBARHEIGHT, KScreenWidth, KScreenHeight+KSTATUBARHEIGHT) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"f3f7f9"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:self.collectionView];
    
    // 注册cell
    [self.collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    //  注册头部试图
    [self.collectionView registerClass:[HomeCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeCollectionHeader"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell getBgImage:self.arrImage[indexPath.row]];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionHeader * headerV = (HomeCollectionHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeCollectionHeader" forIndexPath:indexPath];
    headerV.delegate = self;
    return headerV;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:{
            MyinviteNotesMainController * myinviteVC = [[MyinviteNotesMainController alloc] init];
            [self.navigationController pushViewController:myinviteVC animated:YES];
        }
            break;
        case 1:{
            if (![Tool checkUserIsLogin]) {
                [self.navigationController pushViewController:[JFLoginViewController new] animated:YES];
                return;
            }
            AccountViewController * accVC = [[AccountViewController alloc] init];
            [self.navigationController pushViewController:accVC animated:YES];
        }
            break;
        case 2:{
            JCRepaymentViewController * JCRepayVC = [[JCRepaymentViewController alloc] init];
            [self.navigationController pushViewController:JCRepayVC animated:YES];
        }
            break;
        case 3:{

            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [hud hideAnimated:YES afterDelay:1.f];
            [SHUserCenterHttp finderSHUserAuthStatusSuccess:^(int status) {
                
                SHCashUnionPaymentController * VC = [[SHCashUnionPaymentController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
                
            } failure:^(int status) {
                [self.navigationController pushViewController:[[SHRealNameViewController alloc]init] animated:YES];
            }];
            
        }
            break;
        case 4:{
            CreditViewController * creditVC = [[CreditViewController alloc] init];
            [self.navigationController pushViewController:creditVC animated:YES];
        }
            break;
        case 5:{
            ApplyViewController * creditVC = [[ApplyViewController alloc] init];
            [self.navigationController pushViewController:creditVC animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)arrImage{
    if (!_arrImage) {
        _arrImage = [NSMutableArray arrayWithObjects:@"invite",@"profit",@"energy",@"cashier",@"card_center",@"supermarket", nil];
    }
    return _arrImage;
}


- (void)didBannerAction:(NSInteger)index{
    if (index == 0) {
        JCRepaymentViewController * JCRepayVC = [[JCRepaymentViewController alloc] init];
        [self.navigationController pushViewController:JCRepayVC animated:YES];
    }else{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud hideAnimated:YES afterDelay:1.f];
        [SHUserCenterHttp finderSHUserAuthStatusSuccess:^(int status) {
            
            SHCashUnionPaymentController * VC = [[SHCashUnionPaymentController alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
            
        } failure:^(int status) {
            [self.navigationController pushViewController:[[SHRealNameViewController alloc]init] animated:YES];
        }];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end

